<?php
namespace AlbumTest\Model;

use PHPUnit_Framework_TestCase;
use Zend\Db\ResultSet\ResultSet;
use Album\Model\AlbumTable;
use Album\Model\Album;

class AlbumTableTest extends PHPUnit_Framework_TestCase
{
	public function testFetchAllReturnsAllAlbums()
	{
		$resultSet = new ResultSet();
		$mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway', array('select'), array(), '', false);
		
		$mockTableGateway->expects($this->once())
		                 ->method('select')
		                 ->with()
		                 ->will($this->returnValue($resultSet));
		
		$albumTable = new AlbumTable($mockTableGateway);
		$this->assertSame($resultSet, $albumTable->fetchAll());
	}
	
	public function testCanRetrieveAnAlbumByItsId()
	{
		$album = new Album();
		$album->exchangeArray(array(
		    'id' => 123,
		    'artist' => 'The Military Wives',
		    'title' => 'In My Dreams'		    
		));
		$resultSet = new ResultSet();
		$resultSet->setArrayObjectPrototype(new Album());
		$resultSet->initialize(array($album));
		
		$mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway', array('select'), array(), '', false);
		$mockTableGateway->expects($this->once())
		                 ->method('select')
		                 ->with(array('id' => 123))
		                 ->will($this->returnValue($resultSet));
		$albumTable = new AlbumTable($mockTableGateway);
		$this->assertSame($album, $albumTable->getAlbum(123));
	}
	
	public function testCanDeleteAnAlbumByItsId()
	{
	    $mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway', array('delete'), array(), '', false);
	    $mockTableGateway->expects($this->once())
                	    ->method('delete')
                	    ->with(array('id' => 2));
	    $albumTable = new AlbumTable($mockTableGateway);
	    $albumTable->deleteAlbum(2);
	}
	
	public function testSaveAlbumWillInsertNewAlbumsIfTheyDontAlreadyHaveAnId()
	{
		$albumData = array('artist' => 'The Military Wives', 'title' => 'In My Dreams');
		$album = new Album();
		$album->exchangeArray($albumData);
		$mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway', array('insert'), array(), '', false);
	    $mockTableGateway->expects($this->once())
	                     ->method('insert')
	                     ->with($albumData);
	    $albumTable = new AlbumTable($mockTableGateway);
	    $albumTable->saveAlbum($album);
	}
}
?>